package com.gds.wiki.cloud.admin.server.modular.sys.service;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gds.wiki.cloud.admin.client.sys.model.AdminUser;
import com.gds.wiki.cloud.admin.client.sys.model.Dept;
import com.gds.wiki.cloud.admin.client.sys.model.SysUserRole;
import com.gds.wiki.cloud.admin.client.sys.model.query.AdminUserQuery;
import com.gds.wiki.cloud.admin.client.sys.vo.DeptVo;
import com.gds.wiki.cloud.admin.server.modular.sys.mapper.AdminUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AdminUserService extends ServiceImpl<AdminUserMapper, AdminUser> {

      @Autowired
      private AdminUserMapper userMapper;

      @Autowired
      private SysUserRoleService sysUserRoleService;

      public List<Map> findUserResourceByUid(Long uid) {
            return userMapper.findUserResourceByUid(uid);
      }

      public Page listByDept(AdminUserQuery userQuery) {
            return userMapper.listByDept(new Page<>(userQuery.getPage(), userQuery.getSize()),userQuery);
      }

    @Transactional
     public Boolean changeStatus(String ids, Integer type) {
       Wrapper<AdminUser> updateWrapper=new UpdateWrapper();
        AdminUser user=new AdminUser();
       user.setStatus(type);
       int rs=userMapper.update(user,((UpdateWrapper<AdminUser>) updateWrapper).ne("status",type).in("id",ids.split(",")));

        return rs>0?true:false;
      }

       public Boolean authUser(Long id, String ids) {
       int rs=userMapper.DelUserRole(id);
        List<SysUserRole> list=new ArrayList<>();
        if(StrUtil.isNotEmpty(ids)){
            String[] rids=ids.split(",");
            for(String rid:rids){
                SysUserRole sysUserRole=new SysUserRole();
                sysUserRole.setUserId(id);
                sysUserRole.setRoleId(Long.valueOf(rid));
                list.add(sysUserRole);
            }
            sysUserRoleService.saveBatch(list);
        }
        return true;
    }

    @Transactional
    public Boolean delBatch(String ids) {
        Boolean flag=true;
         Wrapper<AdminUser> updateWrapper=new UpdateWrapper<>();
        ((UpdateWrapper<AdminUser>) updateWrapper).set("status",-1).in("id",ids.split(","));
        Boolean rs=   this.update(updateWrapper);
        if(rs){
            //删除sys_user_role关联表
            Wrapper delWrapper=new UpdateWrapper<>();
            ((UpdateWrapper) delWrapper).in("user_id",ids.split(","));
           rs= sysUserRoleService.remove(delWrapper);
            if(rs){
                return  false;
            }
        }else {
            return  false;
        }
        return flag;
    }

        public List<DeptVo> deptTree(List<Dept> list, DeptVo parentDeptVo){
        List<DeptVo> tree=new ArrayList<>();
          for(Dept item:list){
              if(item.getPid().equals(parentDeptVo.getId())){
                  List<Long> pids=new ArrayList<>();
                  if(parentDeptVo.getPids()!=null&&parentDeptVo.getId()!=0){
                      for(Long a:parentDeptVo.getPids()){
                          pids.add(a);
                      }
                  }else {
                      List<Long> pidArray =new ArrayList<>();
                      pidArray.add(parentDeptVo.getId());
                      parentDeptVo.setPids(pidArray);
                  }
                  pids.add(parentDeptVo.getId());
                  DeptVo deptVo=new DeptVo();
                  deptVo.setPids(pids);
                  BeanUtils.copyProperties(item,deptVo);
                  List<DeptVo> child=deptTree(list,deptVo);
                  deptVo.setChildren(child.size()>0?child:null);
                  tree.add(deptVo);
              }
          }
//
//          list.forEach(item->{
//
//          });

          return tree;
    }
}
